package cn.ch.train.business.service;

import cn.ch.train.business.domain.DailyTrainStation;
import cn.ch.train.business.domain.DailyTrainStationExample;
import cn.ch.train.business.mapper.DailyTrainStationMapper;
import cn.ch.train.business.request.DailyTrainStationQueryParam;
import cn.ch.train.business.request.DailyTrainStationSaveParam;
import cn.ch.train.business.response.DailyTrainStationQueryResult;
import cn.ch.train.common.response.PageResult;
import cn.ch.train.common.util.SnowUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DailyTrainStationService {
    private static final Logger logger = LoggerFactory.getLogger(DailyTrainStationService.class);
    @Resource
    private DailyTrainStationMapper dailyTrainStationMapper;

    public void save(DailyTrainStationSaveParam param) {
        DateTime now = DateTime.now();
        DailyTrainStation dailyTrainStation = BeanUtil.copyProperties(param, DailyTrainStation.class);
        if (Objects.isNull(dailyTrainStation.getId())) {
            dailyTrainStation.setId(SnowUtil.getSnowFlakeNextId());
            dailyTrainStation.setCreateTime(now);
            dailyTrainStation.setUpdateTime(now);
            dailyTrainStationMapper.insert(dailyTrainStation);
        } else {
            dailyTrainStation.setUpdateTime(now);
            dailyTrainStationMapper.updateByPrimaryKey(dailyTrainStation);
        }
    }

    public PageResult<DailyTrainStationQueryResult> queryList(DailyTrainStationQueryParam param) {
        DailyTrainStationExample dailyTrainStationExample = new DailyTrainStationExample();
        dailyTrainStationExample.setOrderByClause("id desc");
        DailyTrainStationExample.Criteria criteria = dailyTrainStationExample.createCriteria();
        if (ObjUtil.isNotNull(param.getDate())) {
            criteria.andDateEqualTo(param.getDate());
        }
        if (ObjUtil.isNotEmpty(param.getTrainCode())) {
            criteria.andTrainCodeEqualTo(param.getTrainCode());
        }
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<DailyTrainStation> dailyTrainStationList = dailyTrainStationMapper.selectByExample(dailyTrainStationExample);

        PageInfo<DailyTrainStation> dailyTrainStationPageInfo = new PageInfo<>(dailyTrainStationList);
        logger.info("总行数：{}", dailyTrainStationPageInfo.getTotal());
        logger.info("总页数：{}", dailyTrainStationPageInfo.getPages());
        List<DailyTrainStationQueryResult> dailyTrainStationQueryList = BeanUtil.copyToList(dailyTrainStationList, DailyTrainStationQueryResult.class);
        PageResult<DailyTrainStationQueryResult> pageResult = new PageResult<>();
        pageResult.setTotal(dailyTrainStationPageInfo.getTotal());
        pageResult.setList(dailyTrainStationQueryList);
        return pageResult;
    }

    public void delete(Long id) {
        dailyTrainStationMapper.deleteByPrimaryKey(id);
    }
}
